﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Threading;
using SDD.Properties;
using System.Diagnostics;

namespace SDD
{
    class Program
    {
        //public static string url = "http://www10.masterlink.com.tw/z/zg/zg_A_0_2.asp.htm";

        public static string FileDir = Settings.Default.FileDir;
        // 系統日可能因為放假而日期不相同
        // QueryData會改變DataDay資料
        public static string DataDay = DateTime.Today.Year.ToString("0000") + DateTime.Today.Month.ToString("00") + DateTime.Today.Day.ToString("00");

        static void Main(string[] args)
        {
            //SayHello();
            DirectoryInfo dir = new DirectoryInfo(FileDir);
            if (dir.Exists == false)
            {
                dir.Create();
            }

            try
            {
               
                //Thread newThread7 = new Thread(new ReadWebPage().Read);
                //newThread7.SetApartmentState(ApartmentState.STA);
                //newThread7.Start();
                //while (newThread7.IsAlive == false) ;
                //newThread7.Join();

                /////////////////////////////////////////////////////////////////////////////////////////
                //測試程式
                //Thread newThread1 = new Thread(new Class1().mytest);
                //newThread1.SetApartmentState(ApartmentState.STA);
                //newThread1.Start();
                //while (newThread1.IsAlive == false) ;
                //newThread1.Join();

                ///////////////////////////////////////////////////////////////////////////////////////////
                ////兩日漲跌幅
                if (Settings.Default.IsGetQueryData)
                {
                    Thread newThread1 = new Thread(new QueryData().AggregateData);
                    newThread1.SetApartmentState(ApartmentState.STA);
                    newThread1.Start();
                    while (newThread1.IsAlive == false) ;
                    newThread1.Join();
                }
                /////////////////////////////////////////////////////////////////////////////////////////
                //類股資金比(已經不抓了)
                //Thread newThread2 = new Thread(new FundRate().AggregateData);
                //newThread2.SetApartmentState(ApartmentState.STA);
                //newThread2.Start();
                //while (newThread2.IsAlive == false) ;
                //newThread2.Join();

                /////////////////////////////////////////////////////////////////////////////////////////
                //類股資金流向
                if (Settings.Default.IsGetFundFlow)
                {
                    Thread newThread2 = new Thread(new FundFlow().AggregateData);
                    newThread2.SetApartmentState(ApartmentState.STA);
                    newThread2.Start();
                    while (newThread2.IsAlive == false) ;
                    newThread2.Join();
                }
                /////////////////////////////////////////////////////////////////////////////////////////
                //每一分鐘委託成交統計
                if (Settings.Default.IsGetTrdePerMin)
                {
                    Thread newThread3 = new Thread(new TrdePerMin().AggregateData);
                    newThread3.SetApartmentState(ApartmentState.STA);
                    newThread3.Start();
                    while (newThread3.IsAlive == false) ;
                    newThread3.Join();
                }
                ///////////////////////////////////////////////////////////////////////////////////////////
                ////台指期未平倉量
                if (Settings.Default.IsGetTaifex)
                {
                    Thread newThread4 = new Thread(new Taifex().AggregateData);
                    newThread4.SetApartmentState(ApartmentState.STA);
                    newThread4.Start();
                    while (newThread4.IsAlive == false) ;
                    newThread4.Join();
                }
                /////////////////////////////////////////////////////////////////////////////////////////
                //召開股東常(臨時)會日期
                if (Settings.Default.IsGetBoardMeetingData)
                {
                    Thread newThread5 = new Thread(new BoardMeetingData().AggregateData);
                    newThread5.SetApartmentState(ApartmentState.STA);
                    newThread5.Start();
                    while (newThread5.IsAlive == false) ;
                    newThread5.Join();
                }
                /////////////////////////////////////////////////////////////////////////////////////////
                //強勢股
                if (Settings.Default.IsGetStrongListData)
                {
                    Thread newThread6 = new Thread(new StrongList().AggregateData);
                    newThread6.SetApartmentState(ApartmentState.STA);
                    newThread6.Start();
                    while (newThread6.IsAlive == false) ;
                    newThread6.Join();
                }
                /////////////////////////////////////////////////////////////////////////////////////////
                //臺指選擇權行情表
                if (Settings.Default.IsGetOptionDayLog)
                {
                    Thread newThread7 = new Thread(new OptionDayLog().AggregateData);
                    newThread7.SetApartmentState(ApartmentState.STA);
                    newThread7.Start();
                    while (newThread7.IsAlive == false) ;
                    newThread7.Join();
                }
                /////////////////////////////////////////////////////////////////////////////////////////
                Console.Write("完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.Write("錯誤");
            }

            //判斷要不要等著按enter
            if (Settings.Default.IsWaitRead)
            {
                Console.WriteLine("，請按enter結束!!");
                Console.Read();
            }

            //判斷要不要打開資料夾
            if (Settings.Default.IsOpenFile)
            {
                OpenFolder();
            }

        }

        /// <summary>
        /// 提示修改項目
        /// </summary>
        public static void SayHello()
        {
            Console.WriteLine("請記得修改app.config FileDir - Value 調整資料儲存位置");
            Console.WriteLine("請記得修改app.config BoardMeetingYear - Value 調整董監改選年度，改為0則不截取董監改選資料");
        }

        /// <summary>
        /// 開啟檔案總管
        /// </summary>
        public static void OpenFolder()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe");
            //  或 直接指定 資料夾路徑    startInfo.Arguments = "C:\\TEST\\";        
            //startInfo.Arguments = "/n";         
            startInfo.Arguments = FileDir;
            Process.Start(startInfo);
        }

    }
}
